package io.renren.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.sp.dao.SpMatchItemDao;
import io.renren.modules.sp.dao.SpMatchItemTeamDao;
import io.renren.modules.sp.entity.SpMatchItemEntity;
import io.renren.modules.sp.entity.SpMatchItemTeamEntity;
import io.renren.modules.sp.service.SpMatchItemTeamService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成参赛编号
 * @author Mark sunlightcs@gmail.com
 */
@Component("generateCertificateTask")
public class GenerateCertificateTask implements ITask{
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private SpMatchItemTeamDao spMatchItemTeamDao;
	@Autowired
	private SpMatchItemTeamService spMatchItemTeamService;
	@Autowired
	private SpMatchItemDao spMatchItemDao;

	@Override
	public void run(String params){
		logger.debug("TestTask定时任务正在执行，参数为：{}", params);
		this.generateCertificate();
	}

	private void generateCertificate(){

		// 查询国赛20下的所有赛项
		List<SpMatchItemEntity> itemList = spMatchItemDao.selectList(new QueryWrapper<SpMatchItemEntity>()
				.eq("match_id", 20)
		);

		List<Long> itemIds = new ArrayList<>();
		for (SpMatchItemEntity item : itemList){
			itemIds.add(item.getId());
		}

		// 查询审核通过的数据 1,2,4,5,6
		// 0审核中 1报名成功 2完赛 3已取消 4比赛中 5已获晋级资格 6晋级成功 7弃权 8驳回 9待确认 10报名未成功
		List<SpMatchItemTeamEntity> matchItemTeamEntityList = spMatchItemTeamDao.selectList(
				new QueryWrapper<SpMatchItemTeamEntity>()
						.in("status", 1,2,4,5,6)
						//.in("match_item_id", itemIds)
						//.in("match_item_id", 74,77)
						//.eq("group_id", 100301)
						.eq("team_id", 1218)
		);
		for (SpMatchItemTeamEntity team : matchItemTeamEntityList){
			SpMatchItemTeamEntity entity = spMatchItemTeamService.selectById(team.getId());
			// 审批后生成比赛证件
			try {
				String errorMsg = spMatchItemTeamService.generateTicketNo(entity.getId());
				if (StringUtils.isNotBlank(errorMsg)) {
					System.out.println("生成证件失败:" + errorMsg);
				}
			}catch (Exception e){
				e.printStackTrace();
				System.out.println("生成证件失败:" + e.getMessage());
			}
		}
	}



}